package struts2.action;

import java.awt.Checkbox;
import java.beans.Statement;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import DAO.Dao_Programme;
import Entity.Programme;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.inject.util.Function;


public class ProgrammeBusiness {
	
	public String getName() {
		return name;
		
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	public String getDes() {
		return des;
	}

	public void setDes(String des) {
		this.des = des;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Programme getMe() {
		return me;
	}

	public void setMe(Programme me) {
		this.me = me;
	}
	private String name;
	private String contact;
	private String des;
	private boolean active;
	Programme me;
	private List<Programme> prlist;
	private String message;
	private String newProgrammeName;
	private int newContactID;
	private String newDescription;
	private boolean includeInActive;

	public ProgrammeBusiness() {
		super();
	}

	// ===========================================
	// Show defaul list programme
	public String showDefaultList() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listDefaultProgramme(true);
		return "success";
	}

	// ===========================================
	// Show all programme
	public String showAllProgramme() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.getProgram();
		return "success";
	}

	// ===========================================
	// Show detail programme
	public String showDetailProgramme() {
		Dao_Programme dao = new Dao_Programme();
		me = dao.getProgramme(name);
//		contactID = p.getContactID();
//		description = p.getDescription();
		return "success";
	}

	// ===========================================
	// Show Programmes By Letter From Fisrt Letter To Second Letter
	// Search programmeName by letter from 0-9
	public String searchListProgrammeByLetterFrom0To9() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listProgrammeByLetterFromFisrtLetterToSecondLetter("0","9");
		ArrayList<Programme>delete = new ArrayList<Programme>();
		for(Programme program:prlist){
			if(!includeInActive && program.isActive()==false){
				System.out.println(program);
				delete.add(program);
			}
		}
		prlist.removeAll(delete);
		ActionContext.getContext().put("typeList", "0T9");
		return "success";
	}

	// Search programmeName by letter from A-E
	public String searchListProgrammeByLetterFromAToE() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listProgrammeByLetterFromFisrtLetterToSecondLetter("a", "e");
		ArrayList<Programme>delete = new ArrayList<Programme>();
		for(Programme program:prlist){
			if(!includeInActive && program.isActive()==false){				
				delete.add(program);
			}
		}
		prlist.removeAll(delete);

		ActionContext.getContext().put("typeList", "aTe");
		return "success";
	}

	// Search programmeName by letter from F-J
	public String searchListProgrammeByLetterFromFToJ() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listProgrammeByLetterFromFisrtLetterToSecondLetter("f",
				"j");

		ArrayList<Programme>delete = new ArrayList<Programme>();
		for(Programme program:prlist){
			if(!includeInActive && program.isActive()==false){
				
				delete.add(program);
			}
		}
		prlist.removeAll(delete);
		ActionContext.getContext().put("typeList", "fTj");
		return "success";
	}

	// Search programmeName by letter from K-N
	public String searchListProgrammeByLetterFromKToN() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listProgrammeByLetterFromFisrtLetterToSecondLetter("k","n");
		
		ArrayList<Programme>delete = new ArrayList<Programme>();
		for(Programme program:prlist){
			if(!includeInActive && program.isActive()==false){
				
				delete.add(program);
			}
		}
		prlist.removeAll(delete);
		ActionContext.getContext().put("typeList", "kTn");
		return "success";
	}

	// Search programmeName by letter from O-R
	public String searchListProgrammeByLetterFromOToR() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listProgrammeByLetterFromFisrtLetterToSecondLetter("o","r");
		ArrayList<Programme>delete = new ArrayList<Programme>();
		for(Programme program:prlist){
			if(!includeInActive && program.isActive()==false){
				
				delete.add(program);
			}
		}
		prlist.removeAll(delete);
		ActionContext.getContext().put("typeList", "oTr");
		return "success";
	}

	// Search programmeName by letter from S-V
	public String searchListProgrammeByLetterFromSToV() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listProgrammeByLetterFromFisrtLetterToSecondLetter("s","v");

		ArrayList<Programme>delete = new ArrayList<Programme>();
		for(Programme program:prlist){
			if(!includeInActive && program.isActive()==false){
				
				delete.add(program);
			}
		}		
		prlist.removeAll(delete);
			
		
		ActionContext.getContext().put("typeList", "sTv");
		return "success";
	}

	// Search programmeName by letter from W-Z
	public String searchListProgrammeByLetterFromWToZ() {
		Dao_Programme dao = new Dao_Programme();
		prlist = dao.listProgrammeByLetterFromFisrtLetterToSecondLetter("w","z");
		
		ArrayList<Programme>delete = new ArrayList<Programme>();
		for(Programme program:prlist){
			if(!includeInActive && program.isActive()==false){
				
				delete.add(program);
			}
		}
		prlist.removeAll(delete);
		
		ActionContext.getContext().put("typeList", "wTz");
		return "success";
	}

	// ===========================================

	// Add an active programme

//	public String addActiveProgramme() {
//		String result = "fail";
//		Dao_Programme dao = new Dao_Programme();
//
//		if ("".equals(programmeName)) {
//			this.message = "Please enter programmeName field!";
//			return "fail";
//		}
//		if (!"".equals(programmeName)
//				&& (!dao.checkProgrammeName(programmeName))) {
//			Programme p = new Programme();
//			p.setProgrammeName(programmeName);
//			p.setDescription(description);
//			p.setContactID(contactID);
//			p.setActive(true);
//
//			dao.addProgramme(p);
//
//			prlist = dao.listProgramme();
//			result = "success";
//		} else {
//			this.message = "This programmeName existed!";
//			return "fail";
//		}
//		return result;
//	}

	// ---------------------------------------
	// Add an inactive programme

//	public String addInActiveProgramme() {
//		String result = "fail";
//		Dao_Programme dao = new Dao_Programme();
//
//		if ("".equals(programmeName)) {
//			this.message = "Please enter programmeName field!";
//			return "fail";
//		}
//		if (!"".equals(programmeName)
//				&& (!dao.checkProgrammeName(programmeName))) {
//			Programme p = new Programme();
//			p.setProgrammeName(programmeName);
//			p.setDescription(description);
//			p.setContactID(contactID);
//			p.setActive(false);
//
//			dao.addProgramme(p);
//			prlist = dao.listProgramme();
//			result = "success";
//		} else {
//			this.message = "This programmeName existed!";
//			return "fail";
//		}
//		return result;
//	}

	// =================================================
	// Amend a programme_active
//	public String amendProgramme_active() {
//		String result = "fail";
//		Dao_Programme dao = new Dao_Programme();
//
//		Programme oldPro = dao.getProgramme(programmeName);
//
//		if ("".equals(newProgrammeName)) {
//			this.message = "Please enter programmeName field!";
//			return "fail";
//		}
//		if ((!"".equals(newProgrammeName))
//				&& (newProgrammeName != programmeName)) {
//			if (!dao.checkProgrammeName(newProgrammeName)) {
//				Programme newPro = new Programme();
//				newPro.setProgrammeName(newProgrammeName);
//				newPro.setContactID(newContactID);
//				newPro.setDescription(newDescription);
//				newPro.setActive(true);
//
//				dao.amendProgramme(oldPro, newPro);
//				prlist = dao.listProgramme();
//				result = "success";
//			} else {
//				this.message = "This programmeName existed!";
//				return "fail";
//			}
//		}
//		return result;
//	}

	// ================================================
	// Amend a programme_deactive

//	public String amendProgramme_deactive() {
//		String result = "fail";
//		Dao_Programme dao = new Dao_Programme();
//
//		Programme oldPro = dao.getProgramme(programmeName);
//		if ("".equals(newProgrammeName)) {
//			this.message = "Please enter programmeName field!";
//			return "fail";
//		}
//		if ((!"".equals(newProgrammeName))
//				&& (newProgrammeName != programmeName)) {
//			if (!dao.checkProgrammeName(newProgrammeName)) {
//				Programme newPro = new Programme();
//				newPro.setProgrammeName(newProgrammeName);
//				newPro.setContactID(newContactID);
//				newPro.setDescription(newDescription);
//				newPro.setActive(false);
//
//				dao.amendProgramme(oldPro, newPro);
//				prlist = dao.listProgramme();
//				result = "success";
//			} else {
//				this.message = "This programmeName existed!";
//				return "fail";
//			}
//		}
//		return result;
//	}

	// =================================================
	// Get/Set Operations
	public String getProgrammeName() {
		return name;
	}

	public void setProgrammeName(String name) {
		this.name = name;
	}


	public String getDescription() {
		return des;
	}

	public void setDescription(String description) {
		this.des = description;
	}

	public List<Programme> getPrlist() {
		return prlist;
	}

	public void setPrlist(List<Programme> prlist) {
		this.prlist = prlist;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNewProgrammeName() {
		return newProgrammeName;
	}

	public void setNewProgrammeName(String newProgrammeName) {
		this.newProgrammeName = newProgrammeName;
	}

	public int getNewContactID() {
		return newContactID;
	}

	public void setNewContactID(int newContactID) {
		this.newContactID = newContactID;
	}

	public String getNewDescription() {
		return newDescription;
	}

	public void setNewDescription(String newDescription) {
		this.newDescription = newDescription;
	}

	public boolean getIncludeInActive() {
		
		return includeInActive;
	}

	public void setIncludeInActive(boolean includeInActive) {
		System.out.println(includeInActive);
		this.includeInActive = includeInActive;
	}

	
	
	
}
